package com.nanjing.east.cloud.batch;

import com.nanjing.east.cloud.alipay.AlipayClient;
import com.nanjing.east.cloud.dao.PaySpecification;
import com.nanjing.east.cloud.domain.PayOrder;
import com.nanjing.east.cloud.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Component
@Slf4j
public class AlipayOrderJob {

    @Autowired
    private OrderService orderService;
    @Autowired
    private AlipayClient alipayClient;
    @Autowired
    private PaySpecification paySpecification;

    ExecutorService executor = Executors.newFixedThreadPool(5);        //创建含5条线程的线程池


    /**
     * 查询5分钟之前的数据
     */
    @Scheduled(cron = "*/5 * * * * ?")
    public void refreshOrder() {
        Date now = new Date();
        long time = 60 * 60 * 1000;//5分钟
        Date beforeDate = new Date(now.getTime() - time);
        List<PayOrder> payOrderList = paySpecification.search(2, beforeDate, 0,3);
        if (payOrderList != null && payOrderList.size() > 0) {
            payOrderList.stream().forEach(e -> {
                executor.submit(new Callable<Object>() {
                    public Object call() {
                        alipayClient.refreshOrder(e.getPayOrderNo());
                        return 1;
                    }
                });

            });
        }
    }
}
